//
//  MineEditPasswordViewController.m
//  BabyAndBaby
//
//  Created by 范琦 on 16/8/2.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MineEditPasswordViewController.h"
#import "MineEditPasswordView1.h"
#import "MBProgressHUD.h"

@interface MineEditPasswordViewController ()
{
    
    MBProgressHUD *hud;
    
}

@property (nonatomic, strong) MineEditPasswordView1 *editView;
@property (nonatomic, strong) UIView *mainView;


@end

@implementation MineEditPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarController.tabBar.hidden = YES;
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    [self.view addSubview:_mainView];
    _mainView.backgroundColor = HUIBACKGROUNDCOLOR;
    self.title = @"修改密码";
    [self creatEditView];
    self.view.backgroundColor = HUIBACKGROUNDCOLOR;
    [self creatHud];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;

}
- (void)creatHud
{
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    
    hud = [[MBProgressHUD alloc] initWithWindow:window];
    hud.removeFromSuperViewOnHide = YES;
    
    [window addSubview:hud];
    hud.labelText = @"两次密码输入不一致";
    hud.labelColor = [UIColor colorWithHexString:@"#A1A1AC"];
    hud.labelFont = [UIFont systemFontOfSize:15];
    hud.mode = MBProgressHUDModeCustomView;
    hud.progress = 0;
    
    
}
- (void)creatEditView
{
    self.editView = [[[NSBundle mainBundle] loadNibNamed:@"MineEditPasswordView1" owner:nil options:nil] lastObject];
    _editView.frame = CGRectMake(0, 15, KSCREENWIDTH, 245);
    [self.mainView addSubview:_editView];
    _editView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, _mainView.bounds.size.height - 44 - 64, KSCREENWIDTH, 44);
    btn.backgroundColor = BACKGROUNDCOLOR;
    [btn setTitle:@"确认修改" forState:UIControlStateNormal];
    btn.tintColor = [UIColor whiteColor];
    [_mainView addSubview:btn];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       
        NSString *oldStr = [_editView.oldPasswordText.text substringFromIndex:4];
        NSString *newStr = [_editView.myNewPasswordText.text substringFromIndex:4];
        NSString *enterStr = [_editView.enterPasswordText.text substringFromIndex:6];
        if ([newStr isEqualToString:enterStr]) {
            
            YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"/Auth/changePassword.html" paramDic:@{@"password_old":oldStr, @"password_new":newStr, @"token":TOKEN} RegisterApiMethod:RequestMethodPost];
            [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                
                NSLog(@"%@", request.responseBody);
                if ([request.responseBody[@"error"] integerValue] != 0) {
                    hud.labelText = request.responseBody[@"msg"];
                    [hud show:YES];
                    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(time) userInfo:nil repeats:NO];
                }else{
                
                    hud.labelText = @"修改成功";
                    [hud show:YES];
                    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(time) userInfo:nil repeats:NO];
                    
                    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(diss) userInfo:nil repeats:NO];

                }
                
            } failure:^(YTKBaseRequest *request) {
                
            }];
        }else{
            
            [hud show:YES];
            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(time) userInfo:nil repeats:NO];
        }
        
    }];
    
    [[_editView.oldPasswordText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        NSString *str = [_editView.oldPasswordText.text substringFromIndex:4];
        if (str.length != 0) {
            _editView.oldPasswordText.text = str;
        }else{
            _editView.oldPasswordText.text = @"";

        }
    }];
    
    [[_editView.oldPasswordText rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
        NSString *str = _editView.oldPasswordText.text;
        _editView.oldPasswordText.text = [NSString stringWithFormat:@"原密码:%@", str];
    }];
    
    [[_editView.myNewPasswordText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        NSString *str = [_editView.myNewPasswordText.text substringFromIndex:4];
        if (str.length != 0) {
            _editView.myNewPasswordText.text = str;
        }else{
            _editView.myNewPasswordText.text = @"";
            
        }
    }];
    
    [[_editView.myNewPasswordText rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
        NSString *str = _editView.myNewPasswordText.text;
        _editView.myNewPasswordText.text = [NSString stringWithFormat:@"新密码:%@", str];
    }];
    
    
    [[_editView.enterPasswordText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        NSString *str = [_editView.enterPasswordText.text substringFromIndex:6];
        if (str.length != 0) {
            _editView.enterPasswordText.text = str;
        }else{
            _editView.enterPasswordText.text = @"";
            
        }
    }];
    
    [[_editView.enterPasswordText rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
        NSString *str = _editView.enterPasswordText.text;
        _editView.enterPasswordText.text = [NSString stringWithFormat:@"确认新密码:%@", str];
        
    }];
    
}
- (void)time
{
    [hud hide:YES];
}
- (void)diss
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
